#include<iostream>
using namespace std;
int main()
{
	long long int a, b, c, k;
	while(cin >> a >> b >> c >> k)
	{
		if(a >= k) cout << k << endl;
		else
		{
			if(b >= k - a) cout << a << endl;
			else cout << a - (k - a - b) << endl;
		}
	}
	return 0;
}
